<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>月考2</title>
    <style>
        #box {
            height: 300px;
            width: 400px;
            border: 2px solid black;
        }

        #comments {
            height: 40px;
            width: 250px;
            border: 2px solid black;
            margin: 10px;
            font-size: 30px;
        }

        #send {
            height: 40px;
            width: 110px;
        }

        #list {
            font-size: 20px;
            list-style-type: none;
        }

        .delete {
            color: blueviolet;
            cursor: pointer;
            margin-right: 20px;
        }

        li {
            display: flex;
            justify-content: space-between;
        }
    </style>
</head>

<body>
    <div id="box">
        <input type="text" id="comments"><button id="send">发布</button>
        <ul id="list">
        </ul>
    </div>
    <script>
        let commentsEle = document.getElementById("comments");
        let sendEle = document.getElementById("send");
        let listELe = document.getElementById("list");


        sendEle.addEventListener("click", function () {
            let comment = commentsEle.value;
            commentsEle.value = "";
            listELe.innerHTML += `<li>${comment}<span class="delete">删除</span></li>`;

            let deleteEle = document.getElementsByClassName("delete");

            [...deleteEle].forEach((deEle) => {
                deEle.addEventListener("click", function () {
                    this.parentElement.remove();
                });
            });
        });
    </script>
</body>

</html>